package com.gzz.orchid.common.webmvc.support;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;

/**
 * @author higmd
 * http 请求结果响应统一格式
 */
public class HttpResult extends HashMap<String, Object> {
    /**
     * 构造函数
     */
    public HttpResult() {
        put("code", 0);
        put("message", "成功");
        put("timestamp", LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()); // 毫秒数时间戳（13位）
    }

    /**
     * 默认成功响应
     *
     * @return
     */
    public static HttpResult success() {
        return new HttpResult();
    }

    /**
     * 成功响应, 自定义信息
     *
     * @param msg 自定义成功信息
     * @return
     */
    public static HttpResult success(String msg) {
        return result(ResultCode.SUCCESS.code, msg);
    }

    /**
     * 默认失败响应
     *
     * @return
     */
    public static HttpResult failure() {
        return result(ResultCode.NORMAL.code, ResultCode.NORMAL.msg);
    }

    /**
     * 失败响应, 自定义信息
     *
     * @param msg 自定义失败信息
     * @return
     */
    public static HttpResult failure(String msg) {
        return result(ResultCode.NORMAL.code, msg);
    }

    public static HttpResult failure(ResultCode ret) {
        return result(ret.code(), ret.msg());
    }

    /**
     * 自定义应响
     *
     * @param code
     * @param msg
     * @return
     */
    public static HttpResult result(int code, String msg) {
        HttpResult httpResult = new HttpResult();
        httpResult.put("code", code);
        httpResult.put("message", msg);
        return httpResult;
    }

    /**
     * 设置数据包
     *
     * @param data
     * @return
     */
    public HttpResult data(Object data) {
        super.put("data", data);
        return this;
    }

    /**
     * 分页
     *
     * @param pager
     * @return
     */
    public HttpResult pager(Object pager) {
        super.put("pager", pager);
        return this;
    }

    /**
     * 设置信息
     *
     * @param msg
     * @return
     */
    public HttpResult message(String msg) {
        super.put("message", msg);
        return this;
    }

    /**
     * 自定义设置
     *
     * @param key
     * @param val
     * @return
     */
    public HttpResult put(String key, Object val) {
        super.put(key, val);
        return this;
    }

    /**
     * 设置签名串
     *
     * @param signStr
     * @return
     */
    public HttpResult sign(String signStr) {
        super.put("sign", signStr);
        return this;
    }


    // 响应代码
    public enum ResultCode {
        /* 成功状态码 */
        SUCCESS(0, "操作成功"),
        /* NONE */
        NORMAL(99, "常规异常"),

        /* 授权权限错误 状态码 */
        AUTHENTICATION_TIMEOUT(100, "授权过时"),
        AUTHENTICATION_FAILED(101, "授权失败"),
        PERMISSION_DENIED(102, "权限被拒绝"),
        //
        PARAM_ERROR(301, "参数异常"),
        BIZZ_ERROR(302, "业务异常 "),
        NOT_FUND(303, "找不到资源"),
        NOT_FUND_DATA(304, "数据不存"),
        /**/
        MQ_EXCEPTION(800, "MQ错误"),
        RPC_EXCEPTION(801, "RPC调用错误"),
        CONF_ERROR(802, "配置错误"),
        REDIS_ERROR(803, "Redis错误"),
        DATABASE_ERROR(804, "数据库异常"),
        //
        UNKNOWN_ERROR(999, "未知异常");

        //        SUCCESS(200, "操作成功"),
        //        CREATED(201, "对象创建成功"),
        //        ACCEPTED(202, "请求已经被接受"),
        //        NO_CONTENT(204, "操作已经执行成功，但是没有返回数据"),
        //        MOVED_PERM(301, "资源已被移除"),
        //        SEE_OTHER(303, "重定向"),
        //        NOT_MODIFIED(304, "资源没有被修改"),
        //        BAD_REQUEST(400, "参数列表错误（缺少，格式不匹配）"),
        //        UNAUTHORIZED(401, "未授权"),
        //        FORBIDDEN(403, "访问受限，授权过期"),
        //        NOT_FOUND(404, "资源，服务未找！"),
        //        BAD_METHOD(405, "不允许的http方法"),
        //        CONFLICT(409, "资源冲突，或者资源被锁"),
        //        UNSUPPORTED_TYPE(415, "不支持的数据，媒体类型"),
        //        ERROR(500, "系统内部错误"),
        //        NOT_IMPLEMENTED(501, "接口未实现"),
        //        WARN(601,"系统警告消息");

        private final int code;
        private final String msg;

        ResultCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int code() {
            return this.code;
        }

        public String msg() {
            return this.msg;
        }


    }
}
